var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var do_App = sm("do_App");
// 实例化一个BaiduLocation对象
var dingwei = sm("do_BaiduLocation");

// UI
var map = ui("do_BaiduMapView_1");
var headerAout = ui("headerAout");
// 变量
var mark1 = "";
var defaultX = "114.045159";
var defaultY = "22.572664";
var iconUrl = "source://image/mark.png";
var dingweiX, dingweiY;
var dingweiCity, dingweiAddress,dingweiDistrict;

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
headerRight.visible = true;
labelNext.visible = true;
headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");

header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "地图",
	"labelNext" : "确认"
})
header.refreshData();

headerRight.on("touch", function() {
	map.removeAll();
	var params={'dingweiX':dingweiX,'dingweiY':dingweiY,'dingweiCity':dingweiCity,'dingweiAddress':dingweiAddress,'dingweiDistrict':dingweiDistrict};
	do_App.closePage(params);
})

// 接收到定位/更新定位时候触发
dingwei.locate("high", function(data, e) {
	if ("4.9E-324" != data.latitude) {// 我的位置,定位成功
		dingweiX = data.longitude;
		dingweiY = data.latitude;
		// 设置固定的经纬度为地图中心点
		map.setCenter({
			latitude : data.latitude,
			longitude : data.longitude
		});
		dingwei.reverseGeoCode(data.latitude, data.longitude, function(data1,
				e1) {
			dingweiCity = data1.city;
			dingweiAddress = data1.address;
			dingweiDistrict=data1.district;
			mark1 = [ {
				"id" : "addCMarkers1",
				"latitude" : data.latitude,
				"longitude" : data.longitude,
				"url" : iconUrl,
				"info" : dingweiAddress
			} ];
			map.addMarkers({
				data : mark1
			})
		});
	}
	if ("4.9E-324" == data.latitude) {// 定位失败
		dingweiX = defaultY;
		dingweiY = defaultX;
		map.setCenter({
			latitude : defaultY,
			longitude : defaultX
		});
		dingwei.reverseGeoCode(defaultY, defaultX, function(data1, e1) {
			dingweiCity = data1.city;
			dingweiAddress = data1.address;
			dingweiDistrict=data1.district;
			mark1 = [ {
				"id" :"addCMarkers1",
				"latitude" : defaultY,
				"longitude" : defaultX,
				"url" : iconUrl,
				"info" : dingweiAddress
			} ];
			map.addMarkers({
				data : mark1
			})
		});
	}
});
map.on("touchMap", function(data, e) {
	map.removeMarker({
		ids : [ "addCMarkers1" ]
	});
	dingwei.reverseGeoCode(data.latitude, data.longitude, function(data1, e1) {
		dingweiCity = data1.city;
		dingweiAddress = data1.address;
		dingweiX = data.longitude;
		dingweiY = data.latitude;
		dingweiDistrict=data1.district;
		mark1 = [ {
			"id" : "addCMarkers1",
			"latitude" : data.latitude,
			"longitude" : data.longitude,
			"url" : iconUrl,
			"info" : dingweiAddress
		} ];
		map.addMarkers({
			data : mark1
		});
	});
});
